/**
 * Copyright © 2025, ZhiYun Network Co.,Ltd. All rights reserved.
 * ZhiYun PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.itcoon.cloud.framework.security.advice;

import com.itcoon.cloud.framework.security.config.HttpSecurityAdvice;
import com.itcoon.cloud.framework.security.config.properties.AuthEntrypointManageProperties;
import com.itcoon.cloud.framework.security.filter.JwtAuthenticationTokenFilter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * ClassName:com.itcoon.cloud.framework.security.advice.JwtHttpSecurityAdvice
 * Description:
 *
 * Date: 1/4/25 3:44 AM
 *
 * @author: miaoxia.yu
 *
 * @version 1.0.0
 */
public class JwtHttpSecurityAdvice implements HttpSecurityAdvice {

    private final AuthenticationEntryPoint authenticationEntryPoint;
    private final UserDetailsService userDetailsService;

    public JwtHttpSecurityAdvice(AuthenticationEntryPoint authenticationEntryPoint, UserDetailsService userDetailsService) {
        this.authenticationEntryPoint = authenticationEntryPoint;
        this.userDetailsService = userDetailsService;
    }

    @Override
    public void advice(AuthEntrypointManageProperties.Path path, HttpSecurity http) throws Exception {
        http.mvcMatcher(path.getBasic())
                .authorizeRequests()
                .and()
                .addFilterBefore(new JwtAuthenticationTokenFilter(authenticationEntryPoint, userDetailsService, path), UsernamePasswordAuthenticationFilter.class);
    }
}
